// createRouter 用于创建路由的函数
// createWebHashHistory  用于创建哈希路由的函数
import { createRouter, createWebHashHistory } from 'vue-router'
// 创建路由与组件的映射关系
import Layout from '@/views/Layout.vue'
import Home from '@/views/home/index.vue'
import CategoryIndex from '@/views/category/index.vue'
import CategorySub from '@/views/category/sub.vue'
import GoodsDetail from '@/views/goods/index.vue'
import Login from '@/views/login/index.vue'
import LoginCallBack from '@/views/login/callback.vue'
import Cart from '@/views/cart/index.vue'
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '', component: Home },
      { path: 'category/:id', component: CategoryIndex },
      { path: 'category/sub/:id', component: CategorySub },
      { path: 'product/:id', component: GoodsDetail }, // 二级分类页
      { path: 'cart', component: Cart }
    ]
  },
  { path: '/login', component: Login },
  { path: '/login/callback', component: LoginCallBack }

]
// 创建实例案例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // 路由进阶 - 滚动行为
  scrollBehavior (to, from, savedPosition) {
    // console.log(to.path)
    // console.log(savedPosition)
    if (to.path !== '/') {
      // 始终滚动到顶部
      return { top: 0 }
      // Vue2返回的是 { x:0 , y:0 }
      // Vue3返回的是 { left:0 , top:0 }
    } else {
      return savedPosition
    }
  }
})
// 导出实例
export default router
